import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/mode/master/file_loxen.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/widget/my_switch.dart';
import 'package:provider/provider.dart';

class DialogUtil {




  static Widget _defaultCover() {
    return Positioned(
      bottom: 0,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(5),
        child: Container(
            width: 322.w,
            height: 208.h,
            color: Color(0xFF0CB7D9),
            child: Align(
              child: Image.asset(
                'images/default_folder.png',
                width: 27.5,
              ),
            )),
      ),
    );
  }

  static Widget editContentDialog(BuildContext context, String title, String hint) {
    TextEditingController _textController = TextEditingController();
    return AlertDialog(
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(bottom: 30.h),
            alignment: Alignment.topLeft,
            child: Text(
              title,
              style: TextStyle(
                fontSize: 32.sp,
                fontWeight: FontWeight.bold,
                color: Theme.of(context).textTheme.subtitle2.color,
              ),
            ),
          ),
          Container(
            width: 560.w,
            height: 360.h,
            color: Color(0xFFF2F2F2),
            child: TextField(
              keyboardType: TextInputType.multiline,
              maxLines: 10,
              //不限制行数
              controller: _textController,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.only(left: 20.w, top: 30.h),
                hintText: hint,
                border: InputBorder.none,
                isDense: true,
              ),
              style: TextStyle(color: Colors.black45, fontSize: 28.sp),
            ),
          ),
        ],
      ),
      actions: <Widget>[
        FlatButton(
          child: Text(StringResources.qu_xiao),
          onPressed: () => Navigator.of(context).pop(),
        ),
        FlatButton(
          child: Text(StringResources.que_ding),
          onPressed: () {
            Navigator.pop(context, _textController.text);
          },
        ),
      ],
    );
  }

  static Widget showMessageDialog(BuildContext context, String title, String message) {
    return AlertDialog(
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(bottom: 30.h),
            alignment: Alignment.topLeft,
            child: Text(
              title,
              style: TextStyle(
                fontSize: 32.sp,
                fontWeight: FontWeight.bold,
                color: Theme.of(context).textTheme.subtitle2.color,
              ),
            ),
          ),
          Container(
            alignment: Alignment.topLeft,
            padding: EdgeInsets.only(
              top: 6.h,
              bottom: 6.h,
            ),
            child: Text(
              message,
              style: TextStyle(
                fontSize: 28.sp,
                color: Theme.of(context).textTheme.subtitle2.color,
              ),
            ),
          ),
        ],
      ),
      actions: <Widget>[
        FlatButton(
          child: Text(StringResources.qu_xiao),
          onPressed: () => Navigator.of(context).pop(),
        ),
        FlatButton(
          child: Text(StringResources.que_ding),
          onPressed: () {
            Navigator.pop(context, true);
          },
        ),
      ],
    );
  }

  static Widget _renderEditCover(String path) {
    return Stack(
      alignment: Alignment.center,
      overflow: Overflow.visible,
      children: <Widget>[
        path.trim() != ''
            ? Positioned(
                bottom: 0,
                child: ClipRRect(
                    borderRadius: BorderRadius.circular(15.w),
                    child: Image.network(
                      path,
                      fit: BoxFit.cover,
                      width: 322.w,
                      height: 208.h,
                    )),
              )
            : _defaultCover(),
        Container(
          color: Colors.transparent,
          width: double.infinity,
          height: 139.h,
        ),
      ],
    );
  }

  static Widget getChangeFileNameDialog(BuildContext context, FileLoxen fileLoxen,
      {Function(bool change, String name) onChange, bool showPrivate = true}) {
    TextEditingController controller = TextEditingController();
    controller.text = fileLoxen.name;
    ValueNotifier<bool> select = ValueNotifier(!fileLoxen.isHide ?? true);
    return Dialog(
      child: Container(
        color: Theme.of(context).backgroundColor,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            _renderEditCover(fileLoxen.cover),
            Container(
              width: double.infinity,
              padding: EdgeInsets.only(top: 55.h, left: 68.w, right: 68.w),
              child: SizedBox(
                height: 65.h,
                child: TextField(
                  controller: controller,
                  style: TextStyle(
                      fontSize: 24.sp, color: Theme.of(context).textTheme.subtitle2.color),
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.fromLTRB(20.w, 5.h, 20.w, 0),
                    hintText: "请输入文件夹名称",
                    border: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0xFF999999), width: 0.5),
                      borderRadius: BorderRadius.all(Radius.circular(0)),
                    ),
                    focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0xFF999999), width: 0.5),
                        borderRadius: BorderRadius.all(Radius.circular(0))),
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0xFF999999), width: 0.5),
                        borderRadius: BorderRadius.all(Radius.circular(0))),
                  ),
                ),
              ),
            ),
            showPrivate
                ? Container(
                    margin: EdgeInsets.only(top: 30.h, left: 60.w),
                    width: double.infinity,
                    height: 50.h,
                    child: Row(
                      children: <Widget>[
                        ValueListenableProvider.value(
                          value: select,
                          builder: (context, _) {
                            return MySwitch(
                                activeColor: Theme.of(context).primaryColor,
                                value: context.watch<bool>(),
                                onChanged: (value) {
                                  select.value = value;
                                });
                          },
                        ),
                        Text(
                          '设为公开',
                          style: TextStyle(
                            fontSize: 28.sp,
                            color: Color(0xFF999999),
                          ),
                        )
                      ],
                    ),
                  )
                : Constants.empty,
            GestureDetector(
              onTap: () {
                if (onChange != null) onChange.call(!select.value, controller.text);
                Navigator.pop(context);
              },
              child: Container(
                  width: 240.w,
                  height: 80.h,
                  color: Color(0xFF0CABCB),
                  alignment: Alignment.center,
                  margin: EdgeInsets.fromLTRB(0, 50.h, 0, 50.h),
                  child: Text(
                    StringResources.que_ding,
                    style: TextStyle(
                      color: Color(0xFFF7F8F8),
                      fontSize: 30.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  )),
            )
          ],
        ),
      ),
    );
  }

  static Widget getCreateFileDialog(BuildContext context,
      {Function(bool select, String name) onCreate, bool showPrivate = true}) {
    TextEditingController controller = TextEditingController();
    ValueNotifier<bool> select = ValueNotifier(false);
    return AlertDialog(
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(bottom: 30.h),
            alignment: Alignment.topLeft,
            child: Text(
              StringResources.xin_jian_wen_jian_jia,
              style: TextStyle(
                fontSize: 32.sp,
                fontWeight: FontWeight.bold,
                color: Theme.of(context).textTheme.subtitle2.color,
              ),
            ),
          ),
          Container(
            height: 66.h,
            padding: EdgeInsets.only(
              top: 6.h,
              bottom: 6.h,
//              left: 11.h
            ),
            decoration: BoxDecoration(border: Border.all(color: Color.fromRGBO(231, 231, 231, 1))),
            child: TextField(
              controller: controller,
              style: TextStyle(
                fontSize: 28.sp,
                color: Theme.of(context).textTheme.subtitle2.color,
              ),
              decoration: InputDecoration(
                hintText: "请输入文件夹名称",
                helperStyle: TextStyle(color: Color(0xBBC0C9), fontSize: 14.sp),
                contentPadding: EdgeInsets.fromLTRB(20.w, 5.h, 20.w, 0),
                border: InputBorder.none,
                isDense: true,
              ),
            ),
          ),
          showPrivate
              ? Container(
                  margin: EdgeInsets.only(top: 30.h),
                  width: double.infinity,
                  height: 50.h,
                  child: Row(
                    children: <Widget>[
                      ValueListenableProvider.value(
                        value: select,
                        builder: (context, _) {
                          return MySwitch(
                              activeColor: Theme.of(context).primaryColor,
                              value: context.watch<bool>(),
                              onChanged: (value) {
                                select.value = value;
                              });
                        },
                      ),
                      Text(
                        '设为公开',
                        style: TextStyle(
                          fontSize: 28.sp,
                          color: Color(0xFF999999),
                        ),
                      )
                    ],
                  ),
                )
              : Constants.empty,
          GestureDetector(
            onTap: () {
              if (onCreate != null) onCreate.call(select.value, controller.text);
              Navigator.pop(context);
            },
            child: Container(
                width: 240.w,
                height: 80.h,
                color: Color(0xFF0CABCB),
                alignment: Alignment.center,
                margin: EdgeInsets.fromLTRB(0, 50.h, 0, 0.h),
                child: Text(
                  StringResources.chuang_jian,
                  style: TextStyle(
                    color: Color(0xFFF7F8F8),
                    fontSize: 30.sp,
                    fontWeight: FontWeight.w400,
                  ),
                )),
          )
        ],
      ),
    );
  }
}
